<?php
unset($text,$s_text);

// Straipsnio ID
if (isset($url['s']) && isnum($url['s']) && $url['s'] > 0) { $sid = (int)$url['s']; } else { $sid = 0; }

// Temos ID
if (isset($url['t']) && isnum($url['t']) && $url['t'] > 0) { $tid = (int)$url['t']; } else { $tid = 0; }

// Tutorialo ID
if (isset($url['p']) && isnum($url['p']) && $url['p'] > 0) { $pid = (int)$url['p']; } else { $pid = 0; }

// Trinamo komentaro ID
if (isset($url['d']) && isnum($url['d']) && $url['d'] > 0) { $did = (int)$url['d']; } else { $did = 0; }

// Trinamo komentaro userID
if (isset($url['u']) && isnum($url['u']) && $url['u'] > 0) { $uid = (int)$url['u']; } else { $uid = 0; }

// Siunciamas komentaras atitinkamas tutorialui
if (isset($_POST['action']) && $_POST['action'] == 'post_send') {
	if (isset($_SESSION['username'])) {
		$zinute = input($_POST['postas']);
		$autorius = $_SESSION['username'];
		$autorius_id = $_SESSION['id'];
		$result = $db->uzklausa("INSERT INTO `s_kom` (`sid`, `autorius`, `autorius_id`, `text`, `data`) VALUES (
							" . escape($pid) .", " . escape($autorius) .", " . escape($autorius_id) .", " . escape($zinute) .", NOW())");
		if (!$result) {
			klaida("Informacija","<center>Klaida įrašant duomenis. Jūsų pranešimas nebuvo įrašytas.</center>");
		}
		$db->uzklausa("UPDATE users SET taskai=taskai+1 WHERE nick=" . escape($_SESSION['username']) ." AND `id` = " . escape($_SESSION['id']) ."");
		header("Location: ?id,22;p," . $pid .";t," . $tid ."");
		unset($zinute,$autorius,$result,$taskai);
	}
	else {
		header("Location: ?home");
	}
}

// Trinamas pasirinktas Straipsnio komentaras
if ($pid != 0 && $tid != 0 && $did != 0 && $uid != 0 && LEVEL == 30) {
	$db->uzklausa("DELETE FROM s_kom WHERE sid=".escape($pid)." AND ID=".escape($did)."");
	$db->uzklausa("UPDATE users SET taskai=taskai-2 WHERE `id`=" . escape($uid) ."");
	header("Location: ?id,22;p," . $pid .";t," . $tid ."");
}

// Jei neperziurimas straipsnis tuomet viskas normaliai ir galim parodyt
if ($pid == 0) {
// Parodomi visi esantys TOPAI Straipsniuose
$text='';$text .= "
		<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
  			<tr>
    			<td width=\"50%\" valign=\"top\">
    			<fieldset>
    			<legend>Naujausi straipsniai</legend>
    ";
    			$sql = $db->uzklausa("SELECT date, id, pav, pid FROM straipsniai WHERE rodoma='TAIP' ORDER by date DESC LIMIT 0,5");
    			while ($row = mysql_fetch_assoc($sql)) {
    				$text .= "<a href='?id,22;p," . $row['pid'] .";t," . $row['id'] ."'>" . $row['pav'] . "</a><br>";
    			}
    unset($sql,$row);
  	$text .= "
  				</fieldset>
  				</td>
    			<td width=\"50%\" valign=\"top\">
    			<fieldset>
    			<legend>Populiariausi straipsniai</legend>";
    			$sql = $db->uzklausa("SELECT date, id, pav, pid FROM straipsniai WHERE rodoma='TAIP' ORDER by click DESC LIMIT 0,5");
    			while ($row = mysql_fetch_assoc($sql)) {
    				$text .= "<a href='?id,22;p," . $row['pid'] .";t," . $row['id'] ."'>" . $row['pav'] . "</a><br>";
    			}
    unset($sql,$row);
  	$text .= "
  	            </fledset>
  				</td>
  			</tr>
  	</table>
  	";
  	lentele("TOP Straipsniai" , $text);
  	$text='';
  	// Paieska
	echo "
		<table width=\"100%\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\">
  			<tr>
   				<td colspan=\"3\" valign=\"top\" width=\"20\">
   					<fieldset>
   					<legend>Paieška</legend>
   					Paieškos frazė:
   					<form name=\"search_s\" action=\"\" method=\"post\">
   						<input name=\"zodis\" type=\"text\" value=\"\">
   						<select size=\"1\" name=\"tema\">";
   							$sql = $db->uzklausa("SELECT id, pav FROM s_punktai WHERE sid=".escape($tid)."");
   							while ($row = mysql_fetch_assoc($sql)) {
   								$text .= "<option value=" . $row['id'] .">" . $row['pav'] ."</option>";
   							}
   							unset($row,$sql);
   						echo "
						</select>
   						<input type=\"submit\" value=\"Ieškoti\">
   						<input type=\"hidden\" name=\"action\"  value=\"post_search\" />
					</form>
					</fieldset>
				</td>
  			</tr>
  		</table>
  		<table>
  			<tr>
    			<td valign=\"top\" width=\"20%\">";
    				$sql = $db->uzklausa("SELECT * FROM s_temos");
    				while ($row = mysql_fetch_assoc($sql)) {
    					$text .= "<a href='?id,22;s," . $row['id'] ."'>" . $row['pav'] ."</a><br>";
    				}
    				lentele("Meniu",$text);
    			echo "
    			</td>
    			<td colspan=\"2\" valign=\"top\">";
    				$text = "<div align=\"justify\">
    							Visa pateikta dokumentacija priklauso ".$conf['Pavadinimas']." Komandai. Plačiau apie tai skaitykite
    							taisyklėse.	Apie netinkamą darbą arba klaidas praneškite Administracijai. Visi straipsniai yra nemokami. Mokestis už šių darbų platinimą yra neleistinas.</div>";
    				lentele("Informacija",$text);
    				echo "
  			</tr>
		</table>
	";
}
	unset($sql);$text='';

// Jei pasirinktas MENIU parodome visus PUNKTUS esancius MENIU skiltyje
if ($sid != 0 && $tid == 0) {
	$text='';
	echo "
		<table width=\"100%\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\">
  			<tr>
    			<td valign=\"top\" width=\"30%\">";
    				$sql = $db->uzklausa("SELECT * FROM s_punktai WHERE sid=$sid");
    				while ($row = mysql_fetch_assoc($sql)) {
    					$text .= "<a href='?id,22;s," . $row['sid'] .";t," . $row['id'] ."'>" . $row['pav'] ."</a><br>";
    				}
    				lentele("Potemės", $text);
    				unset($text,$sql,$row);$text='';
    			echo "
    			</td>
    			<td colspan=\"2\" width=\"70%\" valign=\"top\">
				";
				 	$sql = $db->uzklausa("SELECT * FROM straipsniai ORDER BY `date` DESC LIMIT 0,10");
				 	while ($row = mysql_fetch_assoc($sql)) {
				 		$text = "<a href='?id,22;p," . $row['pid'] .";t," . $row['id'] ."'>" . $row['pav'] ."</a><br><i>" . lauzymas($row['t_text']) ."</i><br><span class='comment'><b>Pateikė: " . $row['autorius'] ." Laikas: " . $row['date'] ." Peržiūrėta: " . $row['click'] ."</span></b><br><br>";
    				}
    				lentele("Naujausi Straipsniai",$text);
    				unset($row,$sql);
    			echo "
    				</legend></td>
  			</tr>
		</table>
	";
	unset($sql,$s_text,$text);$text='';
}

// Jei pasirinktas MENIU > Straipsnio tipas  parodome tutorialus :)
if ($sid != 0 && $tid != 0) {
	echo "
		<table width=\"100%\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\">
  			<tr>
    			<td valign=\"top\" width=\"30%\">";
    				$sql = $db->uzklausa("SELECT * FROM s_punktai WHERE sid=".escape($sid)."");
    				while ($row = mysql_fetch_assoc($sql)) {
    					$text .= "<a href='?id,22;s," . $row['sid'] .";t," . $row['id'] ."'>" . $row['pav'] ."</a><br>";
    				}
    				lentele("Potemės", $text);
    				unset($sql,$text);$text='';
    			echo "
    			</td>
    			<td colspan=\"2\" valign=\"top\">
    			";
    				$sql = $db->uzklausa("SELECT * FROM straipsniai WHERE pid=".escape($tid)." AND rodoma='taip' ORDER BY date DESC");
    				while ($row = mysql_fetch_assoc($sql)) {
    					$text .= "<a href='?id,22;p," . $tid .";t," . $row['id'] ."'>" . $row['pav'] ."</a><br><i>" . lauzymas($row['t_text']) ."</i><br><span class='comment'><b>Pateikė: " . $row['autorius'] ." Laikas: " . $row['date'] ." Peržiūrėta: " . $row['click'] ."</span></b>";
    				}
    				lentele("Straipsniai",$text);
    			echo "</td>
  			</tr>
		</table>
	";
	unset($sql,$s_text,$text);$text='';
}

// Jei pasirinkta pagaliau ir tutorialas kuri nori zmogus perziureti parodom:
if ($pid != 0 && $tid != 0) {
	echo "
		<table width=\"100%\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\">
  			<tr>
    			<td colspan=\"2\" valign=\"top\">";
    				$sql = mysql_fetch_assoc($db->uzklausa("SELECT * FROM straipsniai WHERE pid=$pid AND id=$tid AND rodoma='TAIP'"));
    				// Tutorialas
    				$text = "
    				<span class='comment'><b>Autorius: <a href='?id,47;m," . $sql['autorius_id'] ."'>" . $sql['autorius'] ."</a> Data: " . $sql['date'] ." Įvertinta: " . $sql['vote'] ." Paspausta: " . $sql['click'] ."<br></b></span>
    				<br>" . bbcode($sql['f_text']) ."";
    				lentele($sql['pav'],$text);
    				$clicks = $sql['click'] + 1;
    				$db->uzklausa("UPDATE straipsniai SET  click=$clicks");
    				unset($sql,$clicks);
    				// komentarai
					include("priedai/komentarai.php");
					komentarai($tid);
					echo "
    				</td>
  			</tr>
		</table>
	";
	unset($sql,$s_text,$text);
}


?>